using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TrainTicket.VO;

namespace TrainTicket.DAO
{
    public class NewsDAO
    {
        DbConnection _conn;
        public NewsDAO()
        {
            _conn = new DbConnection();
        }

        public DataTable GetAllNews()
        {
            return _conn.ExecuteDataTable("sp_GetAllNews", null);
        }

        public DataTable GetNewsByTitle(string title)
        {
            SqlParameter param = new SqlParameter();

            param = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
            param.Value = title;

            return _conn.ExecuteDataTable("sp_GetNewsByTitle", param);
        }

        public DataSet GetPagingSQL(int currPage, int recodperpage, int Pagesize)
        {
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("@currPage", SqlDbType.Int);
            param[0].Value = currPage;

            param[1] = new SqlParameter("@recodperpage", SqlDbType.Int);
            param[1].Value = recodperpage;

            param[2] = new SqlParameter("@Pagesize", SqlDbType.Int);
            param[2].Value = Pagesize;

            return _conn.ExecuteDataSet("spNews_PhanTrang", param);
        }

        public DataTable GetNewsById(int NewID)
        {
            SqlParameter param = new SqlParameter();

            param = new SqlParameter("@NewId", SqlDbType.Int);
            param.Value = NewID;

            return _conn.ExecuteDataTable("sp_GetNewsById", param);
        }

        public DataTable GetDetailNewsMax()
        {
            return _conn.ExecuteDataTable("sp_GetDetailNewsMax", null);
        }

        public int InsertNews(News news)
        {
            SqlParameter[] param = new SqlParameter[5];

            param[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
            param[0].Value = news.Title;

            param[1] = new SqlParameter("@Description", SqlDbType.NText);
            param[1].Value = news.Description;

            param[2] = new SqlParameter("@NewsContent", SqlDbType.NText);
            param[2].Value = news.NewContent;

            param[3] = new SqlParameter("@Images", SqlDbType.NVarChar, 200);
            param[3].Value = news.Images;

            param[4] = new SqlParameter("@Status", SqlDbType.Int);
            param[4].Value = news.Status;

            return _conn.ExecuteNonQuery("sp_InsertNews", param);
        }

        public int DeleteNews(int NewID)
        {
            SqlParameter[] param = new SqlParameter[1];
            
            param[0] = new SqlParameter("@NewID",SqlDbType.Int);
            param[0].Value = NewID;

            return _conn.ExecuteNonQuery("sp_DeleteNews", param);
        }


        public int UpdateNews(News news)
        {
            SqlParameter[] param = new SqlParameter[6];

            param[0] = new SqlParameter("@NewID", SqlDbType.Int);
            param[0].Value = news.NewID;

            param[1] = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
            param[1].Value = news.Title;
            
            param[2] = new SqlParameter("@Description", SqlDbType.NText);
            param[2].Value = news.Description;

            param[3] = new SqlParameter("@NewsContent", SqlDbType.NText);
            param[3].Value = news.NewContent;

            param[4] = new SqlParameter("@Images", SqlDbType.NVarChar, 200);
            param[4].Value = news.Images;

            param[5] = new SqlParameter("@Status", SqlDbType.Int);
            param[5].Value = news.Status;


            return _conn.ExecuteNonQuery("sp_UpdatetNews", param);
        }
    }
}
